package org.tea.inventory.admin.web.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.admin.web.admin.BaseController;
import org.tea.inventory.base.response.BaseResponse;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.base.util.excel.ExportExcel;
import org.tea.inventory.domain.tea.LogisticsCompany;
import org.tea.inventory.domain.tea.Product;
import org.tea.inventory.domain.tea.LogisticsCompany;
import org.tea.inventory.service.ILogisticsCompanyService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物流公司信息 Controller
 *
 * @author dongxb
 * @date 2025-04-14
 */
@Api(tags = "基础设置-物流公司信息控制器")
@Slf4j
@RestController
@RequestMapping("/admin/logistics-company")
@RequiredArgsConstructor
public class LogisticsCompanyController extends BaseController
{
    @Autowired
    private ILogisticsCompanyService logisticsCompanyService;

    /**
     * 查询物流公司信息列表
     */
    @GetMapping("page")
    @ApiOperation(value = "获取物流公司信息列表")
    @ApiResponses(
            @ApiResponse(code = 0, message = "成功", response = Product.class)
    )
    public BaseResponse page(LogisticsCompany logisticsCompany)
    {
        log.info("logisticsCompany=" + logisticsCompany.toString());
        startPage();
        List<LogisticsCompany> list = logisticsCompanyService.selectLogisticsCompanyList(logisticsCompany);
        return getDataTable(list);
    }

    /**
     * 导出物流公司信息列表
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出物流公司列表")
    public void exportLogisticsCompany(LogisticsCompany logisticsCompany, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        String title = "物流公司列表";
        String header = "物流公司名称,快递查询代码,菜鸟代码,状态";
        String column = "logisticsName,queryCode,rookieCode,statusStr";
        map.put("biaoti", title);
        map.put("header", header);
        map.put("column", column);
        try {
            List<LogisticsCompany> productCategories = logisticsCompanyService.selectLogisticsCompanyList(logisticsCompany);
            List<Map> list = JSONArray.parseArray(JSON.toJSONString(productCategories), Map.class);
            ExportExcel.export(request, response, list, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取物流公司信息详细信息
     */
    @GetMapping("/info")
    @ApiOperation(value = "获取物流公司信息详细信息")
    public BaseResponse info(Long id){
        if(id == null){
            return ResponseUtil.badArgument();
        }

        LogisticsCompany logisticsCompany = logisticsCompanyService.selectLogisticsCompanyById(id);
        return ResponseUtil.ok(logisticsCompany);
    }

    /**
     * 新增物流公司信息
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增物流公司信息")
    public BaseResponse add(@RequestBody LogisticsCompany logisticsCompany)
    {
        log.info("logisticsCompany=" + logisticsCompany.toString());
        /*if (!productService.checkProductCodeUnique(product)) {
            log.error("类别编号唯一校验失败");
            throw  new ApplicationException(-1,"新增商品失败，商品编号" + product.getProductCode() + "已存在");
        }*/

        return ResponseUtil.ok(logisticsCompanyService.insertLogisticsCompany(logisticsCompany));
    }

    /**
     * 修改物流公司信息
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改物流公司信息")
    public BaseResponse edit(@RequestBody LogisticsCompany logisticsCompany)
    {
        log.info("logisticsCompany=" + logisticsCompany.toString());
        return ResponseUtil.ok(logisticsCompanyService.updateLogisticsCompany(logisticsCompany));
    }

    /**
     * 删除物流公司信息
     */
    @PostMapping(value = "/remove")
    @ApiOperation(value = "删除物流公司信息")
    public BaseResponse remove(Long id)
    {
        return ResponseUtil.ok(logisticsCompanyService.deleteLogisticsCompanyById(id));
    }

    /**
     * 更新物流公司状态
     */
    @PostMapping(value = "/updateStatus")
    @ApiOperation(value = "更新物流公司状态")
    public BaseResponse updateStatus(@ApiParam(value = "更新id") Long id , @ApiParam(value = "更新前状态") Integer status)
    {
        return ResponseUtil.ok(logisticsCompanyService.updateStatus(id , status));
    }
}
